package cn.edu.sctu.sctu_lysd.view.act;

import android.app.ProgressDialog;
import android.os.Handler;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.mobsandgeeks.saripaar.Rule;
import com.mobsandgeeks.saripaar.Validator;
import com.mobsandgeeks.saripaar.annotation.ConfirmPassword;
import com.mobsandgeeks.saripaar.annotation.Password;
import com.mobsandgeeks.saripaar.annotation.Required;
import com.mobsandgeeks.saripaar.annotation.TextRule;

import cn.edu.sctu.sctu_lysd.R;
import cn.edu.sctu.sctu_lysd.base.BaseActivity;
import cn.edu.sctu.sctu_lysd.biz.RegisterBiz;
import cn.edu.sctu.sctu_lysd.databinding.ActivityRegisterBinding;
import cn.edu.sctu.sctu_lysd.presenter.RegisterPresenter;
import cn.edu.sctu.sctu_lysd.utils.JumpUtils;
import cn.edu.sctu.sctu_lysd.utils.ProgressUtil;

/**
 * Created by wangfuxin on 2017-04-06.
 */

public class RegisterAct extends BaseActivity implements RegisterBiz.View, Validator.ValidationListener {

    private ActivityRegisterBinding mBinding;
    private RegisterBiz.Presenter mPresenter;
    private ProgressUtil progressUtil;
    private ProgressDialog progressDialog;

    @Required(order = 1)
    @TextRule(order = 2, minLength = 11, maxLength = 11, message = "请输入正确的电话号码！")
    private EditText mRegisterPhone;

    @Required(order = 3, message = "必填选项！")
    private EditText mRegisterCode;

    @Required(order = 4, message = "必填选项！")
    @Password(order = 5)
    private EditText mFirstPasswoerd;

    @Required(order = 6, message = "必填选项！")
    @ConfirmPassword(order = 7, message = "两次密码不一致！")
    private EditText mRegisterPassword;

    private EditText mNickName;

    private Validator validator;

    @Override
    public void initPresenter(RegisterBiz.Presenter presenter) {

        this.mPresenter = presenter;

    }

    @Override
    protected void onResume() {
        super.onResume();
        initPresenter(new RegisterPresenter(this, this));
        mPresenter.start();
    }

    @Override
    public void registerSuccess() {

        progressUtil.dismiss(progressDialog);

        ProgressUtil.showAlertDialog("立即登陆", "注册成功，点击确认登陆", this, new ProgressUtil.AlertDialogCallBack() {
            @Override
            public void submit() {

                JumpUtils.jumpActivity(RegisterAct.this,LoginAct.class);

            }

            @Override
            public void cancel() {

                JumpUtils.jumpActivity(RegisterAct.this,IndexActivity.class);

            }
        }); }

    @Override
    public void registerFailed(int errorCode) {

        progressUtil.dismiss(progressDialog);

        switch (errorCode) {

            case RegisterPresenter.CODE__EXISTS:

                Toast.makeText(this, "该用户已存在", Toast.LENGTH_SHORT).show();

                break;

            case RegisterPresenter.CODE__PHONE_CODE_ERROR:

                Toast.makeText(this, "验证码错误", Toast.LENGTH_SHORT).show();

                break;

            case RegisterPresenter.CODE__NET_ERROR:

                Toast.makeText(this, "网络错误", Toast.LENGTH_SHORT).show();

                break;

            default:
                break;

        }


    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_register;
    }

    @Override
    protected void initAfter(Object bind) {

        this.mBinding = (ActivityRegisterBinding) bind;

        validator = new Validator(this);

        mBinding.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mRegisterPhone = mBinding.idActRegisterPhoneEdit;
        mRegisterCode = mBinding.idActRegisterCodeEdit;
        mFirstPasswoerd = mBinding.idActRegisterPasswordEdit;
        mRegisterPassword = mBinding.idActRegisterConfirmPasswordEdit;

        progressUtil = ProgressUtil.getInstance(this);


        validator.setValidationListener(this);

        mBinding.timeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String phoneNumber = mPresenter.getInputSting(mRegisterPhone);

                if (!phoneNumber.equals("") && phoneNumber.length() == 11) {

                    mBinding.timeBtn.setTextAfter("秒后重新获取");
                    mBinding.timeBtn.setClick();
                    mPresenter.getCode(phoneNumber);
                } else {

                    Toast.makeText(RegisterAct.this, "请填写正确的电话号码", Toast.LENGTH_SHORT).show();
                }


            }
        });

        mBinding.registerSubmitBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                validator.validate();
            }
        });

    }

    @Override
    public void onValidationSucceeded() {

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {

                progressDialog = progressUtil.getProgressDialog(ProgressDialog.STYLE_HORIZONTAL, true, "ssss");

                mPresenter.register(mPresenter.getInputSting(mRegisterPhone),
                        mPresenter.getInputSting(mRegisterCode),
                        mPresenter.getInputSting(mFirstPasswoerd),
                        mPresenter.getInputSting(mRegisterPassword));

            }
        }, 1000);


    }

    @Override
    public void onValidationFailed(View failedView, Rule<?> failedRule) {


        String message = failedRule.getFailureMessage();

        if (failedView instanceof EditText) {
            failedView.requestFocus();
            ((EditText) failedView).setError(message);
        } else {
            Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        mPresenter.cancel();

    }
}
